package de.westranger.forex.code.generation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CreateMQLScript {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final String javaFile = ".\\src\\de\\westranger\\forex\\code\\script\\MQLScriptPrototypeMA.java";
		//final String javaFile = ".\\src\\de\\westranger\\forex\\code\\script\\MQL_SAR_ADX_Trader.java";
		final String mqlFile = ".\\mql\\MQLScriptPrototypeMA.mql";
		
		final String[] remove = new String[] { "private ", "public ",
				"static ", "final " ,"\\(int\\)"};
		final String[][] replace = new String[][] { { "boolean", "bool" },{ "String ", "string " },{"double\\[\\]\\[\\] arrayData;","double arrayData[50][100];"},
				{ "null", "NULL" }, { "MQLFileWriter\\.", "" },{ "MQLIndicator\\.", "" },
				{ "MQLScriptPrototype\\.", "" }, { "Math\\.abs", "MathAbs" },
				{ "Math\\.min", "MathMin" }, { "Math\\.max", "MathMax" }, {"mql_validation_mql","mt4_validation_mql"},
				{ "Math\\.sqrt", "MathSqrt" }, { "\\]\\[", "," } ,{"\\t"," "}};

		StringBuilder sb = new StringBuilder();
		for(String s:remove){
			sb.append(s);
			sb.append('|');
		}

		try {
			BufferedReader br = new BufferedReader(new FileReader(javaFile));
			BufferedWriter bw = new BufferedWriter(new FileWriter(mqlFile));

			while(br.ready()){
				String line = br.readLine() + '\r' +'\n';
				line = line.replaceAll(sb.toString(), "");
				line = line.replaceFirst('\t'+"", "");

				for(String[] s:replace){
					line = line.replaceAll(s[0], s[1]);
				}

				if(!line.contains("REMOVEME") && !line.contains("//")){
					bw.write(line);					
				}
			}

			br.close();
			bw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
